
package com.epam.android.training.day11;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author pavel_busko
 * 
 * Activity to test media player
 */
public class Day11Activity extends Activity {

    private Button buttonStartStop;

    private OnClickListener playStarter;

    private OnClickListener playStoper;

    private boolean isPlaying = false;

    protected boolean isPrepared = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
                R.array.playlist, R.layout.custom_checked_list_view);
        final ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);
        // listView.setItemsCanFocus(false);
        final MediaPlayer mp = new MediaPlayer();
        mp.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                isPrepared = true;
                if (isPlaying) {
                    mp.start();
                }
            }
        });
        preparePlayer(mp, adapter.getItem(0));
        listView.setItemChecked(0, true);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                preparePlayer(mp, adapter.getItem(listView.getCheckedItemPosition()));
                if (isPlaying) {
                    play(mp);
                }
            }
        });

        buttonStartStop = (Button) findViewById(R.id.button_start);
        playStarter = new OnClickListener() {
            @Override
            public void onClick(View v) {
                play(mp);
                switchButtonToStop();
            }
        };
        playStoper = new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
                mp.reset();
                isPlaying = false;
                switchButtonToStart();
            }
        };
        buttonStartStop.setOnClickListener(playStarter);
    }

    private void preparePlayer(final MediaPlayer mp, CharSequence charSequence) {
        isPrepared = false;
        mp.reset();
        setSource(mp, charSequence);
        mp.prepareAsync();
    }

    private void setSource(final MediaPlayer mp, CharSequence charSequence) {
        try {
            mp.setDataSource(charSequence.toString());
        } catch (IllegalStateException e) {
            // see http://code.google.com/p/android/issues/detail?id=957
            mp.reset();
            setSource(mp, charSequence);
        } catch (Throwable e) {
            Toast.makeText(getApplicationContext(), getResources().getString(R.string.problem_getting_mp3_from_url),
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void play(final MediaPlayer mp) {
        isPlaying = true;
        if (isPrepared) {
            mp.start();
        } // else mp.start() will be called by OnPreparedListener
    }

    private void switchButtonToStart() {
        buttonStartStop.setOnClickListener(playStarter);
        buttonStartStop.setText(R.string.button_start_label);
    }

    private void switchButtonToStop() {
        buttonStartStop.setOnClickListener(playStoper);
        buttonStartStop.setText(R.string.button_stop_label);
    }
}
